package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.entity.Order1;
import com.project.entity.RestPOJO;
import com.project.entity.Stall;
import com.project.service.StallService;
import com.project.service.TenantOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(tags="订单生成管理相关")
public class TenantOrderController {
    @Autowired
    private TenantOrderService tenantOrderService;
    @Autowired
    private StallService stallService;
    @GetMapping("/zt_selectOrderAll")
    @ApiOperation(
            value = "查询当前租客所有历史订单",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            //如果是json，指定paramType为body
            @ApiImplicitParam(paramType="query",name="currentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="每页数",required=true),
            @ApiImplicitParam(paramType="query",name="date1",value="查询订单开始时间",required=false),
            @ApiImplicitParam(paramType="query",name="date2",value="查询订单结束时间",required=false),
            @ApiImplicitParam(paramType="query",name="vehicle_code",value="查询租客车牌号",required=false),
            @ApiImplicitParam(paramType="query",name="user_id",value="当前用户id",required=true),
    })
    public RestPOJO zt_selOrderAll(Integer currentPage, Integer pageSize,String date1,String date2,String vehicle_code,int user_id){
        RestPOJO rdt=new RestPOJO();
        Date dt1 = null;
        Date dt2 = null;
        if(date1!=null && date2!=null){
            dt1 = TimeFormat.TimeFormat(date1);
            dt2 = TimeFormat.TimeFormat(date2);
            System.out.println(dt1);
        }
        PageInfo<Order1> pageInfo = tenantOrderService.zt_selOrderAll_Date(currentPage, pageSize, dt1, dt2, vehicle_code,user_id);
//        Date order_startime = pageInfo.getList().get(0).getOrder_startime();
        if(pageInfo!=null&&pageInfo.getList().size()>=0){
            rdt.setMessages("查询订单成功");
            rdt.setStatus(8848);
            rdt.setData(pageInfo);
            System.out.println(rdt.getData());

        }
        else{
            rdt.setMessages("查询失败");
            rdt.setStatus(0000);
        }
        return rdt;
    }
    @GetMapping("/zt_selOrder_conduct")
    @ApiOperation(
            value = "查询当前租客所有正在进行中订单",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            //如果是json，指定paramType为body
            @ApiImplicitParam(paramType="query",name="currentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="每页数",required=true),
            @ApiImplicitParam(paramType="query",name="user_id",value="当前用户id",required=true),
    })
    public RestPOJO zt_selOrder_conduct(Integer currentPage, Integer pageSize,int user_id){
        RestPOJO rdt=new RestPOJO();
        PageInfo<Order1> pageInfo = tenantOrderService.zt_selOrder_conduct(currentPage,pageSize,user_id);
        if(pageInfo!=null&&pageInfo.getList().size()>0){
            rdt.setMessages("查询进行中订单成功");
            rdt.setStatus(8848);
            rdt.setData(pageInfo);
            System.out.println(rdt.getData());

        }
        else{
            rdt.setMessages("查询失败");
            rdt.setStatus(0000);
        }
        return rdt;
    }
    @GetMapping("/zt_Order_Complete01")
    @ApiOperation(
            value = "租客订单确认时根据租用时间返回产生费用",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            //如果是json，指定paramType为body
            @ApiImplicitParam(paramType="query",name="start_time",value="订单开始时间",required=true),
            @ApiImplicitParam(paramType="query",name="stall_id",value="车位id",required=true),
    })
    public RestPOJO zt_Order_Complete01(String start_time,Integer stall_id){
        RestPOJO rdt=new RestPOJO();
        double resTime = TimeCalcu.timeCalcu(start_time);
        Stall stall = stallService.zt_selStallById(stall_id);//查车位费用
        double price =  stall.getStall_price();
        double money1 = price*resTime;
        int money2 = (int)(money1*100);//后面加括号！！
        double money = money2/100.0;//除以100.0！！记得.0！
        System.out.println("==========="+money);
        if(money>=0){//必须加等于号！！
            rdt.setMessages("生成价格成功");
            rdt.setStatus(8848);
            rdt.setData(money);
        }
        else{
            rdt.setMessages("查询失败");
            rdt.setStatus(0000);
        }
        return rdt;
    }
    @PostMapping("/zt_Order_CompleteFinal")
    @ApiOperation(
            value = "订单最终确认 生成最终订单到历史订单",
            response = RestPOJO.class
    )
    public RestPOJO zt_Order_CompleteFinal(@RequestBody @Validated Order1 order1){
        RestPOJO rdt=new RestPOJO();
        Integer order_user_id = order1.getOrder_user_id();
        Integer order_stall_id = order1.getOrder_stall_id();//车位id插数据
        Date order_startime = order1.getOrder_startime();
        System.out.println("============"+order_startime);
        Integer order_id = order1.getOrder_id();//order_id
        System.out.println(order_id+"+"+order_stall_id+"+"+order_startime);//输出
        Date endTime = new Date();//结束时间
        double resTime = TimeCalcu.timeCalcu2(order_startime,endTime);//持续时间
        System.out.println(resTime);//输出
        int i = 0;
        try {
            i = tenantOrderService.zt_DoOrder1End(endTime, order_id, order_stall_id, resTime,order_user_id);
            if(i == 4){
                rdt.setMessages("订单交易成功！");
                rdt.setStatus(8848);
            }else{
                rdt.setMessages("交易 失败！");
                rdt.setStatus(0000);
            }
        } catch (Exception e) {
            rdt.setMessages("扣款失败！");
            rdt.setStatus(0000);
        }finally {
            return rdt;
        }
    }
}
